<template>
	<scroll-view scroll-y="true" class="page">
		<u-navbar :placeholder="true" title="发表动态" @leftClick="$utils.back()" border>

		</u-navbar>
		<view>
			<view class="mt20 flex pb20" style="min-height: 20vh;">
				<u--textarea maxlength='999' v-model="textarea" placeholder="请输入内容" autoHeight></u--textarea>
			</view>
			<view v-if="imageUrl.length==0&&!video.url" class="mt20 upload flex" @click="openSelect">
				<u-icon name="camera-fill" color="#D3D4D6" size="50"></u-icon>
				
			</view>
			<view :class="{mt20:imageUrl.length!=0}" v-if="imageShow">
				<imageUpload :imageShow='imageShow' @changeImageList='changeImageList' :imageUrl='imageUrl'
					@close='imageShow=null' :conquer_id='conquer_id' />
			</view>
			<view :class="{mt20:video.url}" v-if="videoShow">
				<videoUpload @changeVideo='changeVideo' @close='videoShow=false' />
			</view>
			<view style="align-items: center;" class="mt20 borderBottom  flex pb20 pt20">
				<view class="mr20 ">
					步数：
				</view>
				<view style="flex: 1;display: flex;justify-content: flex-end;">
					<u-loading-icon size='42rpx' v-if="setpLoading"></u-loading-icon>
					<template v-else>
						<view v-if="step">{{step}}</view>
						<view v-else class="button" @click="getStep">
							获取步数
						</view>
					</template>
				</view>
			</view>
			<u-popup :show="showSelect" :round="10" @close="showSelect=false">
				<view class="popupTitle">
					<u-icon name="close" color='#fff'></u-icon>
					<view>
						选择方式
					</view>
					<u-icon name="close" @click="showSelect=false"></u-icon>
				</view>
				<view style="height: 20vh;">
					<u-grid :border="false" @click="clickGrid">
						<u-grid-item v-for="(baseListItem,baseListIndex) in baseList" :key="baseListIndex">
							<u-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.name" :size="22"></u-icon>
							<text class="grid-text">{{baseListItem.title}}</text>
						</u-grid-item>
					</u-grid>
				</view>
			</u-popup>
		</view>
		<view class="bottom viewWidth">
			<u-button :disabled="disabled" @click="pulish">发表</u-button>
		</view>
		<hongbao v-if="redPackets.length" :user='redPackets[0].user' :redPackets='redPackets' />
	</scroll-view>
</template>

<script>
	import imageUpload from '@/components/imageUpload';
	import videoUpload from '@/components/videoUpload';
	import hongbao from '@/components/hongbao';
	export default {
		components: {
			hongbao,
			imageUpload,
			videoUpload,
		},
		data() {
			return {
				setpLoading: false,
				redPackets: [],
				step: null,
				showSelect: false,
				select: null,
				imageShow: null,
				videoShow: false,
				baseList: [{
						name: 'photo',
						title: '本地图片'
					},
					{
						name: 'play-right',
						title: '本地视频'
					},
					{
						name: 'chat',
						title: '素材聊天'
					},
				],
				imageUrl: [],
				video: {

				},
				textarea: null,
				disabled: false,
				loading: true,
				openid: uni.getStorageSync('openid')
			}
		},

		onLoad(op) {


		},
		methods: {
			getStep() {
				this.setpLoading = true
				wx.login({
					success: (res) => {
						this.$api.Login.Session({
							code: res.code
						}).then(res => {
							wx.getWeRunData({
								success: (res) => {
									this.$api.Step.Step({
										openid: this.openid,
										encryptedData: res.encryptedData,
										iv: res.iv
									}).then(res => {
										this.setpLoading = false
										this.step = res.data.userData.stepInfoList[
											30].step
									}).catch(err => {
										this.setpLoading = false
										uni.$u.toast('微信步数获取失败')
									})
								},
								fail: () => {
									uni.$u.toast('微信步数获取失败')
								}
							})
						}).catch(err => {
							this.setpLoading = false
							uni.$u.toast('微信步数获取失败')
						})
					},
				})
			},


			changeVideo(e) {
				this.video = e
			},
			openSelect() {
				this.showSelect = true
			},
			clickGrid(e) {
				if (e == 0) {
					this.imageShow = 'photo'
					this.showSelect = false
				} else if (e == 1) {
					this.videoShow = true
					this.showSelect = false
				} else {
					this.imageShow = 'chat'
					this.showSelect = false
				}
			},

			changeImageList(e) {
				this.imageUrl = e
			},
			pulish() {
				if (!this.imageUrl.every(item => item.upload) && this.imageUrl.length) {
					uni.$u.toast('正在上传中')
					return
				}
				if (!this.video.upload && this.video.url) {
					uni.$u.toast('正在上传中')
					return
				}

				if (!this.imageUrl.length && !this.video.id) {
					uni.$u.toast('必须上传图片视频')
					return
				}
				if (!this.textarea) {
					uni.$u.toast('请输入内容')
					return
				}
				if (this.textarea.length < 6) {
					uni.$u.toast('内容长度不能小于6个字符')
					return
				}
				if (!this.step) {
					uni.$u.toast('请获取微信步数')
					return
				}
				this.Add()

			},
			Add() {
				this.disabled = true
				let obj = {
					title: this.textarea,
					images: this.imageUrl.map(item => {
						return {
							url: item.url,
							small_url: item.small_url,
							title: item.title
						}
					}),
					video_ids: this.video.id ? [this.video.id] : [],
					step: this.step
				}
				this.$api.Run.Add(obj).then(res => {
					uni.$u.toast('新增动态成功')
					if(!res.data.redPackets.length){
						uni.reLaunch({
							url:'/run/index'
						})
						return
					}
					this.redPackets = res.data.redPackets.map(item => {
						return {
							...item,
							run_id: res.data.run.id
						}
					})
					this.disabled = false
				}).catch(err => {
					this.disabled = false
					console.log(err)
					uni.$u.toast(err.data.message)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.minus {
		width: 22px;
		height: 22px;
		border-width: 1px;
		border-color: #E6E6E6;
		border-style: solid;
		border-top-left-radius: 100px;
		border-top-right-radius: 100px;
		border-bottom-left-radius: 100px;
		border-bottom-right-radius: 100px;
		@include flex;
		justify-content: center;
		align-items: center;
	}

	.input {
		padding: 0 10px;
	}

	.plus {
		width: 22px;
		height: 22px;
		background-color: #FF0000;
		border-radius: 50%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
		align-items: center;
	}

	.page {
		display: flex;
		height: calc(100vh - 90rpx - constant(safe-area-inset-bottom) + 10rpx);
		height: calc(100vh - 90rpx - env(safe-area-inset-bottom) + 10rpx);
		flex-direction: column;
		padding: 0 20rpx;
		box-sizing: border-box;
	}

	.borderBottom {
		border-bottom: 1rpx solid #f1f1f1;
	}

	





	.flex {
		display: flex;
	}

	.pb20 {
		padding-bottom: 20rpx;
	}

	.pt20 {
		padding-top: 20rpx;
	}

	.upload {
		border-radius: 8rpx;
		background-color: #f5f5f5;
		width: 100%;
		height: 200rpx;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	::v-deep .u-border {
		border-color: #fff !important;
	}

	::v-deep .u-textarea {
		padding: 0 !important;
	}

	.selectItem {
		font-size: 30rpx;
		text-align: center;
		padding: 20rpx 0;
	}

	.selectItem:nth-child(1) {
		border-bottom: 1rpx solid #f1f1f1;
	}

	.userBox {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 20rpx 0;

		view:nth-child(2) {
			flex: 0.98;
		}
	}

	.bottom {
		position: fixed;
		bottom: calc(constant(safe-area-inset-bottom) + 10rpx);
		/* 兼容 iOS 设备 */
		bottom: calc(env(safe-area-inset-bottom) + 10rpx);
		width: calc(100% - 40rpx);
	}



	.button {
		border-radius: 100rpx;
		border: 1rpx solid #bbb;
		padding: 5rpx 10rpx;
		font-size: 24rpx;
	}
</style>